import {
	sub,
	pub
} from '@/common/event.js'
import {
	getRqst
} from '@/common/request.js'
import store from '@/store/index.js'
import websocket from '@/common/websocket.js'
export default {
	namespaced: true,
	state: {
		user: {},
		setting: {

		}
	},
	mutations: {
		setUserState: (state, payload) => {
			state.user = payload
			uni.setStorageSync('user', state.user)
			pub("userInit", state.user)
		},
		clearUserState: (state, payload) => {
			state.user = {}
			uni.removeStorageSync('user')
		},
		setNoteState: (state, payload) => {
			state.user.note = payload
			uni.setStorageSync('user', state.user)
		},
		setAvatarState: (state, payload) => {
			state.user.avatar = payload
			uni.setStorageSync('user', state.user)
		},
		setNameState: (state, payload) => {
			state.user.name = payload
			uni.setStorageSync('user', state.user)
		},
		setSettingState: (state, payload) => {
			state.setting = payload
			state.setting.mute_status = payload.mute_status == 1
			uni.setStorageSync('userSetting', state.setting)
		},
	},
	actions: {
		async setUserAvatar({
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().put(`/user/user/setUserAvatar`, {
				avatar: payload.avatar
			})
			console.log(4422, ret)
			if (ret.success) {
				commit(`setAvatarState`, payload.avatar)
			} else {
				uni.showToast({
					title: ret.message ?? "请求失败",
					icon: "none"
				})
			}
		},
		async setUserNote({
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().put(`/user/user/setUserNote`, {
				note: payload.note
			})
			console.log(4422, ret)
			if (ret.success) {
				commit(`setNoteState`, payload.note)
			} else {
				uni.showToast({
					title: ret.message ?? "请求失败",
					icon: "none"
				})
			}
		},
		async setUserName({
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().put(`/user/user/setUserName`, {
				name: payload.name
			})
			console.log(4422, ret)
			if (ret.success) {
				commit(`setNameState`, payload.name)
			} else {
				uni.showToast({
					title: ret.message ?? "请求失败",
					icon: "none"
				})
			}
		},
		async initSetting({
			commit,
			state
		}, {
			payload
		}) {
			const ret = await getRqst().get(`/user/user/setting/${state.user.id}`, payload)
			if (ret.success) {
				commit(`setSettingState`, ret.data)
			}
		},
		async setUserSetting({
			commit
		}, {
			payload
		}) {
			const ret = await getRqst().put(`/user/user/setUserSetting`, payload)
			console.log(123321, ret)
			if (ret.success) {
				if (typeof payload.mute_status == "number") {
					payload.mute_status = payload.mute_status == 1
				}
				commit(`setSettingState`, payload)
				uni.showToast({
					title: "设置成功",
				})
			} else {
				uni.showToast({
					title: ret.message ?? "请求失败",
					icon: "none"
				})
			}
		},
	},
	init: function() {
		const user = uni.getStorageSync('user')
		console.log(`执行User初始化`, user)
		if (user && user.id) {
			store.commit(`user/setUserState`, user)
			store.dispatch({
				type: `user/initSetting`,
				payload: {}
			})
		}
		sub(`loginSuccess`, function(e) {
			store.commit(`user/setUserState`, e.user)
			store.dispatch({
				type: `user/initSetting`,
				payload: {}
			})
		})
	}
}